﻿using System;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using BLL;
using BLL.Access;
using Common;
using Entity;
using Platform.Common;
using Platform.Permission;
using Platform.Security;

namespace Web
{
    public partial class _Default : Page
    {
        #region 私有变量

        /// <summary>
        ///     显示类型
        /// </summary>
        private static readonly int DisplayType =
            Utility.ToInt(ConfigurationManager.AppSettings["DisplayType"], 0).Value;

        #endregion

        #region 属性

        /// <summary>
        ///     传递过来的用户名
        /// </summary>
        private string UrlUserName
        {
            get
            {
                string username = null;
                if (HttpContext.Current.Request["userC"] != "")
                    username = HttpContext.Current.Request["userC"];
                return username;
            }
        }

        /// <summary>
        ///     JS 字符串 全局变量
        /// </summary>
        public string jqStr = string.Empty;

        #endregion

        #region 事件

        protected void Page_Load(object sender, EventArgs e)
        {
            //登录密码解密
            //string aa = new EncryptAndDecrypt().Decrypt("wj7BsRshc0F8uMR+l3YXEg==");
            //连接字符串加解密
            //string aa = SecurityHelper.DESDecrypt("zSO7k91DzH6CH+9Zzl3hjVXfNyZG7Zojrp+yX+O64kxrHZ1Dfc5VnWpJOIZmCVK+Io3DHUs3Z3daEesvkwreM7li0XVBSrAHWY2ulgoK6Gl7rvqBgpXnFSAWUvwtXCYX7goWlT0CvAbE8NxbTFrTx/btmhYjA0mzohxQ/0K1/U9U9Ja9OYfnBWNevuHJ8mzvYjR5MidEvzASqjc1YBY6CrnjD82X9y+tPebuvscHbVxBwmam1VUKhV5y8n/vFG5kI1zWClHj1DVYwrwSgxVARkXYqmFDuPCTWwRz83NDM3Wj0ga3IjkgnPKr6aBLx1edn9ruFgQ2BTez7mbMPu/WyHjtX3W4i1dv", "shanghai3h");
            var bb = SecurityHelper.DESEncrypt(
                "Data Source=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST =192.168.1.15)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = planwatertes)));User Id=planwater_user;Password=password;Min Pool Size=10;Connection Lifetime=120;Connection Timeout=120;",
                "shanghai3h");
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            //if (Request.Cookies["PlanWater"]["name"] == "116")
            //{
            //    ItemsW.Items.Add("地下水", 2);
            //    ItemsW.Items.Add("地表水", 1);
            //    ItemsW.Items.Add("水资源系统接口", 4);
            //}
            //else
            //{
            //    ItemsW.Items.Add("自来水", 0);
            //    ItemsW.Items.Add("地下水", 2);
            //    ItemsW.Items.Add("地表水", 1);
            //    ItemsW.Items.Add("水资源系统接口", 4);
            //}
            //ItemsW.SelectedIndex = 0;

            if (!IsPostBack)
            {
                if (Request.Cookies["PlanWater"] != null)
                {
                    txtUserName.Value = Request.Cookies["PlanWater"]["name"];
                    //txtPassword.Attributes.Add("value", Request.Cookies["PlanWater"]["pwd"]);
                    var code = Request.Cookies["code"];
                    if (code == null)
                    {
                        ItemsW.SelectedIndex = 0;
                    }
                    else
                    {
                        if (code.Value == "0")
                            ItemsW.SelectedIndex = 0;
                        else if (code.Value == "1")
                            ItemsW.SelectedIndex = 1;
                        else if (code.Value == "2")
                            ItemsW.SelectedIndex = 2;
                        else if (code.Value == "4")
                            ItemsW.SelectedIndex = 3;
                    }
                }

                HttpContext.Current.Session.Clear();
                if (null == UrlUserName)
                {
                }
                else
                {
                    var openWindowsType = ConfigurationManager.AppSettings["OpenWindowsType"];
                    LoginHelper.SaveUserProfile(UrlUserName);

                    //Session["UserStateId"] = BLL.BusinessTool.GetZhanDianBHByYHAccount(UrlUserName);
                    //如果1 表示是转页
                    if (DisplayType == 1)
                        Response.Redirect("DefaultN.aspx");
                    //表示Window.Open
                    else
                        jqStr = "wOpen()";
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            var newcookie = new HttpCookie("code");
            newcookie.Value = ItemsW.SelectedItem.Value.ToString(); //drpWaterItem.SelectedValue
            newcookie.Expires = DateTime.Now.AddYears(1);
            Response.AppendCookie(newcookie);
            //HttpContext.Current.Response.Cookies.Add(newcookie);
            var userName = Request.Form.Get("txtUserName").Trim();
            var userPassword = Request.Form.Get("txtPassword").Trim();
            var match = Regex.Match(userName, @"[0-9a-zA-Z]*${1,}", RegexOptions.IgnoreCase);
            if (match.ToString().Equals(userName) && !string.IsNullOrEmpty(userName.Trim()))
            {
                var encryptAndDecrypt = new EncryptAndDecrypt();
                var encryptPass = encryptAndDecrypt.Encrypt(userPassword.Trim());
                if (LoginHelper.CheckPassword(userName.Trim(), encryptPass))
                {
                    var hck = new HttpCookie("PlanWater");
                    hck.Values.Set("name", userName.Trim());
                    hck.Values.Set("pwd", userPassword.Trim());
                    hck.Expires = DateTime.Now.AddYears(1);
                    Response.AppendCookie(hck);
                    LoginHelper.SaveUserProfile(userName.Trim());
                    var rsno = "-1";
                    var rsmsg = "";
                    var UserId = LoginHelper.GetUserInfo(userName.Trim()).UserID;
                    //获取当前登录人电子签名
                    var dtT = BusinessTool.GetTextAndValue("AS_USERINFO", "USERID", "S_DZQM", null, "USERID=" + UserId)
                        .Tables[0];
                    LoginInfo.CurrentUser.ElectronSign = Convert.ToString(dtT.Rows[0]["VALUE"]);
                    new SpHelper().P_LOGIN(UserId, 1, out rsno, out rsmsg);
                    if (rsno == "-1")
                    {
                        jqStr = "sLogError('" + rsmsg + "')";
                        return;
                    }

                    //BLL.BusinessTool.WType = Convert.ToInt32(this.ItemsW.Value.ToString());////drpWaterItem.SelectedValue
                    Session["WType"] =
                        Convert.ToInt32(ItemsW.SelectedItem.Value.ToString()); ////drpWaterItem.SelectedValue
                    //if (this.drpWaterItem.SelectedIndex == 0)
                    //{
                    //    BLL.BusinessTool.WType = 0;
                    //}
                    //else if (this.drpWaterItem.SelectedIndex == 1)
                    //{
                    //    BLL.BusinessTool.WType = 2;
                    //}
                    //else if (this.drpWaterItem.SelectedIndex == 2)
                    //{
                    //    BLL.BusinessTool.WType = 1;
                    //}
                    //else if (this.drpWaterItem.SelectedIndex == 3)
                    //{
                    //    BLL.BusinessTool.WType = 4;
                    //}
                    //if (drpWaterItem.SelectedValue == "0")
                    //{
                    //    Response.Redirect("DefaultHref.aspx");
                    //}
                    if (rsno == "-1")
                    {
                        jqStr = "sLogError('" + rsmsg + "')";
                        return;
                    }

                    //如果1 表示是转页
                    if (DisplayType == 1)
                        Response.Redirect("Default.aspx");
                    else
                        jqStr = "wOpen()";
                }
                else
                {
                    jqStr = "sError()";
                }
            }
            else
            {
                jqStr = "sError()";
            }
        }

        #endregion
    }
}